package com.igeek.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.igeek.mapper.InsuranceFlowable1Mapper;
import com.igeek.model.claims.Claims;
import com.igeek.model.claims.InsuranceFlowable1;
import com.igeek.service.IInsuranceFlowable1Service;
import com.igeek.vo.process.ResultDto;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-19
 */
@Service
public class InsuranceFlowable1ServiceImpl extends ServiceImpl<InsuranceFlowable1Mapper, InsuranceFlowable1> implements IInsuranceFlowable1Service {
    @Autowired
    private  InsuranceFlowable1Mapper insuranceFlowable1Mapper;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private ClaimsServiceImpl claimsService;
    @Override
    public void agree(Integer id, ResultDto resultDto) {

        InsuranceFlowable1 insuranceFlowable1 =insuranceFlowable1Mapper.selectById(id);
        TaskService taskService = processEngine.getTaskService();
        List<Task> tasks = taskService.createTaskQuery()
                .processInstanceId(insuranceFlowable1.getProcInstId())
                .list();
        for (Task task : tasks) {
            String taskId = task.getId();
            System.out.println(taskId);
            taskService.complete(taskId);
        }
        insuranceFlowable1.setStatus("已同意");
        insuranceFlowable1.setEndTime(resultDto.getEndTime());
        insuranceFlowable1Mapper.updateById(insuranceFlowable1);
        Claims claims =claimsService.getById(insuranceFlowable1.getClaimsId());
        claims.setStatus("审核完成");
        claims.setLoss(resultDto.getLoss());
        claims.setDescription(resultDto.getDescription());
        claimsService.updateById(claims);

    }
}
